﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum ZombieState {
    Standby,
    Move,
    Attack,
    Lost,
    Die
}
public class Zombie : MonoBehaviour {
    public float speedMove = 0.02f;
    public int hp = 100;

    private Animator anim;

    private ZombieState _state = ZombieState.Standby;
    public ZombieState state
    {
        get
        {
            return _state;
        }
        set
        {
            if (value == _state) return;
            _state = value;
            PlayStateAnim();
        }

    }

    private float attackSpeed =1f;

    private SunFlower target;
    private float timer = 0;


    private void Awake()
    {
        anim = GetComponent<Animator>();
    }


    private void Start()
    {
        state = ZombieState.Move;        
    }

    private void Update()
    {
        
        Move();
        Attack();
    }

 

    void Attack()
    {
        if (target == null || state!=ZombieState.Attack) return;
        timer += Time.deltaTime;
        if (timer >= attackSpeed)
        {
            timer = 0;
            target.SubHp(10);
        }


    }


    private void Move()
    {   
        if(state==ZombieState.Move || state == ZombieState.Lost)
        {
            transform.Translate(Vector3.left * speedMove * Time.deltaTime);
        }        
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {             
        string tag = collision.tag;
        switch (tag)
        {
            case "PlantBullet":
                    SubHp(10);
                break;
            case "Plant":
                state = ZombieState.Attack;
                target = collision.gameObject.GetComponent<SunFlower>();
                break;
        }
    }



    private void OnTriggerExit2D(Collider2D collision)
    {
        string tag = collision.tag;
        
        switch (tag)
        {
            case "Plant":               
                state = ZombieState.Move;
                target = null;
                break;
        }

    }


    private void SubHp(int value)
    {
        hp -= value;        
        if (hp <= 0)
        {
            state = ZombieState.Die;
        }else if (hp <= 30)
        {
            state = ZombieState.Lost;
        }
    }


    void PlayStateAnim()
    {
        
        switch (state)
        {
            case ZombieState.Lost:
                //anim.SetBool("Lost", true);
                anim.SetTrigger("Lost");
                break;
            case ZombieState.Attack:
                //anim.SetTrigger("Attack");
                anim.SetBool("Attack", true);
                break;
            case ZombieState.Die:
                anim.SetTrigger("Die");
                //anim.SetBool("Die", true);                
                break;
            case ZombieState.Move:                
                anim.SetBool("Attack", false);
                break;
        }
    }

    private void Destroy()
    {
        GameObject.Destroy(gameObject);
    }

  
}
